﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.Utils.Controls;
using DevExpress.Utils.Drawing;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Ribbon.Gallery;
using TransLogger.Module.BusinessObjects;

namespace TransLogger.Module.Win.Editors
{

    [ListEditor(typeof(BusinessObjects.OperationHistory))]
    public class GalleryListEditor : ListEditor
    {
        private GalleryItemGroup _group;
        private GalleryControl _gallery;
        private object controlDataSource;
        private object _focusedObject;

        public GalleryListEditor(IModelListView info)
            : base(info)
        {

        }

        protected override object CreateControlsCore()
        {
            _gallery = new GalleryControl();
            _gallery.Gallery.ShowGroupCaption = false;
            _gallery.Gallery.ShowItemText = true;
            _gallery.Gallery.ImageSize = new Size(200, 200);
            _gallery.Gallery.ItemImageLayout = ImageLayoutMode.ZoomInside;
            _gallery.KeyDown += GalleryOnKeyDown;
            _gallery.MouseDoubleClick += GalleryOnMouseDoubleClick;
            _gallery.Gallery.ItemClick += GalleryOnItemClick;
            _gallery.Gallery.ItemCheckMode = ItemCheckMode.SingleRadio;
            _gallery.Gallery.ItemCheckedChanged += GalleryOnItemCheckedChanged;

            _group = new GalleryItemGroup();
            _gallery.Gallery.Groups.Add(_group);
            Refresh();
            return _gallery;
        }

        private void GalleryOnItemCheckedChanged(object sender, GalleryItemEventArgs galleryItemEventArgs)
        {
            OnSelectionChanged();
            OnFocusedObjectChanged();
        }

        private void GalleryOnItemClick(object sender, GalleryItemClickEventArgs galleryItemClickEventArgs)
        {
            FocusedObject = galleryItemClickEventArgs.Item.Tag;
        }

        private void GalleryOnMouseDoubleClick(object sender, MouseEventArgs mouseEventArgs)
        {
            if (mouseEventArgs.Button == MouseButtons.Left && FocusedObject!=null)
            {
                OnProcessSelectedItem();
            }
        }

        private void GalleryOnKeyDown(object sender, KeyEventArgs keyEventArgs)
        {
            if (keyEventArgs.KeyCode == Keys.Enter&&FocusedObject!=null)
            {
                OnProcessSelectedItem();
            }
        }

        protected override void AssignDataSourceToControl(object dataSource)
        {
            if (controlDataSource != dataSource)
            {
                var old = controlDataSource as IBindingList;
                if (old != null)
                {
                    old.ListChanged -= DataSourceListChanged;
                }

                controlDataSource = dataSource;
                var bindable = controlDataSource as IBindingList;
                if (bindable != null)
                {
                    bindable.ListChanged += DataSourceListChanged;
                }

                Refresh();
            }
        }

        private void DataSourceListChanged(object sender, ListChangedEventArgs listChangedEventArgs)
        {
            Refresh();
        }

        public override void Refresh()
        {
            if (_gallery == null)
            {
                return;
            }

            try
            {
                _gallery.Gallery.BeginUpdate();
                var oldItems = _group.Items;
                _group.Items.Clear();
                foreach (GalleryItem oldItem in oldItems.Cast<GalleryItem>().Where(oldItem => oldItem.Image != null))
                    oldItem.Image.Dispose();

                var lst = ListHelper.GetList(controlDataSource);
                if (lst != null)
                {
                    var count = 0;
                }
            }
            finally
            {
                _gallery.Gallery.EndUpdate();
            }

            if (FocusedObject == null && _group.Items.Count > 0)
            {
                var first = _group.Items[0];
                first.Checked = true;
                FocusedObject = first.Tag;
            }
        }

        public override IList GetSelectedObjects()
        {
            return _group != null ? _group.GetCheckedItems().Select(i => i.Tag).ToArray() : new object[0];
        }

        public override SelectionType SelectionType
        {
            get { return SelectionType.Full; }
        }

        public override IContextMenuTemplate ContextMenuTemplate
        {
            get { return null; }
        }

        public override object FocusedObject
        {
            get
            {
                System.Diagnostics.Debug.WriteLine(_focusedObject);
                return _focusedObject;
            }
            set
            {
                _focusedObject = value;
            }
        }
    }
}
